<?php
// +----------------------------------------------------------------------
// | SparkShop 坚持做优秀的商城系统
// +----------------------------------------------------------------------
// | Copyright (c) 2022~2099 http://sparkshop.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: NickBai  <1902822973@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\service;

use app\admin\validate\DeviceCateValidate;
use app\model\device\Device;
use app\model\device\DeviceCate;
use think\exception\ValidateException;

class DeviceCateService
{
    /**
     * 添加设备号分类
     * @param array $param
     * @return array
     */
    public function addDeviceCate(array $param): array
    {
        if (isset($param['file'])) {
            unset($param['file']);
        }

        // 检验完整性
        try {
            validate(DeviceCateValidate::class)->check($param);
        } catch (ValidateException $e) {
            return dataReturn(-1, $e->getError());
        }

        $deviceCateModel = new DeviceCate();
        $has = $deviceCateModel->checkUnique([
            'name' => $param['name']
        ])['data'];

        if (!empty($has)) {
            return dataReturn(-2, '分类名已经存在');
        }

        if ($param['pid'] == 0) {
            $param['level'] = 1;
        } else {
            $parentInfo = $deviceCateModel->findOne([
                'id' => $param['pid']
            ], 'pid')['data'];

            if ($parentInfo['pid'] == 0) {
                $param['level'] = 2;
            } else {
                $param['level'] = 3;
            }
        }

        $param['create_time'] = now();
        return $deviceCateModel->insertOne($param);
    }

    /**
     * 编辑设备号分类
     * @param array $param
     * @return array
     */
    public function editDeviceCate(array $param): array
    {
        if (isset($param['file'])) {
            unset($param['file']);
        }

        // 检验完整性
        try {
            validate(DeviceCateValidate::class)->check($param);
        } catch (ValidateException $e) {
            return dataReturn(-1, $e->getError());
        }

        $deviceCateModel = new DeviceCate();

        $where[] = ['id', '<>', $param['id']];
        $where[] = ['name', '=', $param['name']];
        $has = $deviceCateModel->checkUnique($where)['data'];

        if (!empty($has)) {
            return dataReturn(-2, '分类名已经存在');
        }

        // 处理子分类隐藏
        $deviceCateModel->updateByWhere([
            'status' => $param['status'],
            'update_time' => now()
        ], [
            'pid' => $param['id']
        ]);

        return $deviceCateModel->updateById($param, $param['id']);
    }

    /**
     * 删除设备号分类
     * @param int $id
     * @return array
     */
    public function delDeviceCate(int $id): array
    {
        // 查询是否有设备号使用否则不让删
        $deviceModel = new Device();
        $deviceInfo = $deviceModel->findOne([
            'cate_id' => $id,
            'is_del' => 2
        ], 'id')['data'];
        if (!empty($deviceInfo)) {
            return dataReturn(-1, '有设备号在使用该分类，不可删除');
        }

        $deviceCateModel = new DeviceCate();
        $has = $deviceCateModel->findOne([
            'pid' => $id
        ], 'id')['data'];
        if (!empty($has)) {
            return dataReturn(-2, '该分类下有子分类，不可删除');
        }

        $deviceCateModel = new DeviceCate();
        return $deviceCateModel->delById($id);
    }
}